<?php
class DateCommon {
	/**
	 * @author Corey <corey.erp@gmail.com> 2012-11-07
	 *
	 */
	function toDate($time, $format = 'Y-m-d H:i:s') {
		if (empty ( $time )) {
			return '';
		}
		$format = str_replace ( '#', ':', $format );
		return date ($format, $time );
	}
	
	/**
	 * 友好的时间显示
	 * M2
	 * @param int    $sTime 待显示的时间
	 * @param string $type  类型. normal | mohu | full | ymd | other
	 * @param string $alt   已失效
	 * @return string
	 */
	function friendlyDate($sTime,$type = 'normal',$alt = 'false') {
		//sTime=源时间，cTime=当前时间，dTime=时间差
		$cTime		=	time();
		$dTime		=	$cTime - $sTime;
		$dDay		=	intval(date("z",$cTime)) - intval(date("z",$sTime));
		$dYear		=	intval(date("Y",$cTime)) - intval(date("Y",$sTime));
		//normal：n秒前，n分钟前，n小时前，日期
		if($type=='normal'){
			if( $dTime < 60 ){
				return $dTime.L('secondsAgo');
			}elseif( $dTime < 3600 ){
				return intval($dTime/60).L('minutesAgo');
				//今天的数据.年份相同.日期相同.
			}elseif( $dYear==0 && $dDay == 0  ){
				return L('today').date('H:i',$sTime);
			}elseif($dYear==0){
				return date("m月d日 H:i",$sTime);
			}else{
				return date("Y-m-d H:i",$sTime);
			}
		}elseif($type=='mohu'){
			if( $dTime < 60 ){
				return $dTime.L('secondsAgo');
			}elseif( $dTime < 3600 ){
				return intval($dTime/60).L('minutesAgo');
			}elseif( $dTime >= 3600 && $dDay == 0  ){
				return intval($dTime/3600).L('hoursAgo');
			}elseif( $dDay > 0 && $dDay<=7 ){
				return intval($dDay).L('daysAgo');
			}elseif( $dDay > 7 &&  $dDay <= 30 ){
				return intval($dDay/7) . L('weeksAgo');
			}elseif( $dDay > 30 ){
				return intval($dDay/30) . L('monthsAgo');
			}
		}elseif($type=='full'){
			return date("Y-m-d , H:i:s",$sTime);
		}elseif($type=='ymd'){
			return date("Y-m-d",$sTime);
		}else{
			if( $dTime < 60 ){
				return $dTime.L('secondsAgo');
			}elseif( $dTime < 3600 ){
				return intval($dTime/60).L('minutesAgo');
			}elseif( $dTime >= 3600 && $dDay == 0  ){
				return intval($dTime/3600).L('hoursAgo');
			}elseif($dYear==0){
				return date("Y-m-d H:i:s",$sTime);
			}else{
				return date("Y-m-d H:i:s",$sTime);
			}
		}
	}
}

?>